<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'physical:sportsPlan:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ planId: record.planId })">
          {{ record.adaptAcademicYear }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <ModalList @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsPhysicalSportsPlanList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import moment from 'moment';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { sportsPlanDelete, sportsPlanListData } from '/@/api/physical/sportsPlan';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import ModalList from './modalList.vue';

  const { t } = useI18n('physical.sportsPlan');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('体育考核计划管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('适配学年'),
        field: 'adaptAcademicYear',
        component: 'DatePicker',
        componentProps: ({ formModel }) => {
          return {
            mode: 'year',
            allowClear: true,
            format: 'YYYY',
            onChange: (e) => {
              console.log(e);
            },
            onPanelChange: (value) => {
              formModel.adaptAcademicYear = moment(value).format('YYYY');
            },
          };
        },
      },
      {
        label: t('适配年级'),
        field: 'adaptGrade',
        component: 'Select',
        componentProps: {
          dictType: 'vke_grade',
          allowClear: true,
        },
      },
      // {
      //   label: t('状态'),
      //   field: 'status',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'sys_search_status',
      //     allowClear: true,
      //     onChange: handleSuccess,
      //   },
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('适配学年'),
      dataIndex: 'adaptAcademicYear',
      key: 'a.adapt_academic_year',
      sorter: true,
      width: 230,
      align: 'left',
      dictType: '',
      slots: { customRender: 'firstColumn' },
    },
    {
      title: t('适配年级'),
      dataIndex: 'adaptGrade',
      key: 'a.adapt_grade',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'vke_grade',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    // {
    //   title: t('状态'),
    //   dataIndex: 'status',
    //   key: 'a.status',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'sys_search_status',
    // },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑体育考核计划'),
        onClick: handleForm.bind(this, { planId: record.planId }),
        auth: 'physical:sportsPlan:edit',
      },
      {
        icon: 'ant-design:copyright-circle-outlined',
        title: t('复制体育考核计划'),
        onClick: handleCopy.bind(this, { planId: record.planId }),
        auth: 'physical:sportsPlan:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除体育考核计划'),
        popConfirm: {
          title: t('是否确认删除体育考核计划'),
          confirm: handleDelete.bind(this, { planId: record.planId }),
        },
        auth: 'physical:sportsPlan:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload }] = useTable({
    api: sportsPlanListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }
  function handleCopy(record: Recordable) {
    openModal(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await sportsPlanDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
